import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 要根据相应的权限对不同的路由进行拆分
// 常量路由 ：所有人都可见的模块 如首页 404 页面
// 异步路由：不同的用户/角色需要筛选的路由
export const constantRoutes = [{
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard'
      }
    }]
  },

  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]
// 对外暴露异步路由
export const asyncRoutes = [
  // 权限管理
  {
    path: 'acl',
    component: Layout,
    redirect: '/acl',
    name: 'Acl',
    meta: {
      title: '权限管理',
      icon: 'el-icon-lock'
    },
    children: [{
      path: '/user',
      name: 'User',
      component: () => import('@/views/acl/user'),
      meta: {
        title: '用户管理',
      }
    }, {
      path: '/role',
      name: 'Role',
      component: () => import('@/views/acl/role'),
      meta: {
        title: '角色管理',
      }
    }, {
      path: '/permission',
      name: 'Permission',
      component: () => import('@/views/acl/permission'),
      meta: {
        title: '菜单管理',
      }
    }]
  },
  // 商品管理
  {
    path: '/product',
    component: Layout,
    name: 'Product',
    meta: {
      title: '商品管理',
      icon: 'el-icon-goods'
    },
    children: [{
        path: 'trademark',
        name: 'Trademark',
        component: () => import('@/views/product/trandeMark'),
        meta: {
          title: '品牌管理'
        }
      },
      {
        path: 'attr',
        name: 'Attr',
        component: () => import('@/views/product/Attr'),
        meta: {
          title: '品台属性管理'
        }
      },
      {
        path: 'spu',
        name: 'Spu',
        component: () => import('@/views/product/Spu'),
        meta: {
          title: 'Spu管理'
        }
      }, {
        path: 'sku',
        name: 'Sku',
        component: () => import('@/views/product/Sku'),
        meta: {
          title: 'Sku管理'
        }
      },
      {
        path: 'ooo',
        name: 'Ooo',
        component: () => import('@/views/product/Ooo'),
        meta: {
          title: 'OOO管理'
        }
      }
    ]
  },
]
// 任意路由 如404路径出现错误
export const anyRputes = {
  path: '/404',
  component: () => import('@/views/404'),
  hidden: true
}
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
